package com._119yn.host.gst200.cansocket.codec;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.util.CharsetUtil;

public class LineEncoder extends ChannelOutboundHandlerAdapter {

    @Override
    public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
        if (msg instanceof String s) {
            ctx.write(Unpooled.copiedBuffer(s, CharsetUtil.UTF_8));
            ctx.writeAndFlush(Unpooled.copiedBuffer("\n", CharsetUtil.UTF_8));
        } else {
            ctx.write(msg);
        }
    }

}
